package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.databinding.Bindable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;
import com.umeng.socialize.ShareContent;
import com.umeng.socialize.media.UMImage;

import java.util.ArrayList;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.PopupActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * 个人主页
 * Created by zxh on 16/11/29.
 */
public class PersonalHome extends ObservableWithGoBackHandler {
    private int attentioncoun;
    private int fanscoun;
    private double rate;
    private int uid;
    private String background;
    private String userName;
    private String avatar;
    private String job;
    private String realName;
    private String zone;
    private String introduction;
    private String industry;
    private String company;
    private boolean infoSelected;
    private boolean articleSelected;
    private ListBindingAdapter articles;
    private boolean attentioned;
    private boolean expert;


    public int getUid() {
        return uid;
    }

    @Bindable
    public String getBackground() {
        return background;
    }

    public void setBackground(String background) {
        this.background = background;
        notifyPropertyChanged(BR.background);
    }

    @Bindable
    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
        notifyPropertyChanged(BR.company);
    }

    @Bindable
    public boolean isAttentioned() {
        return attentioned;
    }

    public void setAttentioned(boolean attentioned) {
        this.attentioned = attentioned;
        notifyPropertyChanged(BR.attentioned);
    }

    @Bindable
    public ListBindingAdapter getArticles() {
        return articles;
    }

    @Bindable
    public boolean isInfoSelected() {
        return infoSelected;
    }

    public void setInfoSelected(boolean infoSelected) {
        this.infoSelected = infoSelected;
        if (this.infoSelected) {
            setArticleSelected(false);
        }
        notifyPropertyChanged(BR.infoSelected);
    }

    @Bindable
    public boolean isArticleSelected() {
        return articleSelected;
    }

    public void setArticleSelected(boolean articleSelected) {
        this.articleSelected = articleSelected;
        if (this.articleSelected) {
            setInfoSelected(false);
            loadArticle();
        }
        notifyPropertyChanged(BR.articleSelected);
    }

    @Bindable
    public int getAttentioncoun() {
        return attentioncoun;
    }

    public void setAttentioncoun(int attentioncoun) {
        this.attentioncoun = attentioncoun;
        notifyPropertyChanged(BR.attentioncoun);
    }

    @Bindable
    public int getFanscoun() {
        return fanscoun;
    }

    public void setFanscoun(int fanscoun) {
        this.fanscoun = fanscoun;
        notifyPropertyChanged(BR.fanscoun);
    }

    @Bindable
    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
        notifyPropertyChanged(BR.rate);
    }

    @Bindable
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
        notifyPropertyChanged(BR.userName);
    }

    @Bindable
    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
        notifyPropertyChanged(BR.avatar);
    }

    @Bindable
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
        notifyPropertyChanged(BR.job);
    }

    @Bindable
    public boolean isExpert() {
        return expert;
    }

    public void setExpert(boolean expert) {
        this.expert = expert;
        notifyPropertyChanged(BR.expert);
//        notifyPropertyChanged(BR.tag);
    }

    @Bindable
    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
        notifyPropertyChanged(BR.realname);
    }

    @Bindable
    public String getZone() {
        return zone;
    }

    public void setZone(String zone) {
        this.zone = zone;
        notifyPropertyChanged(BR.zone);
    }

    @Bindable
    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
        notifyPropertyChanged(BR.introduction);
    }

    @Bindable
    public String getIndustry() {
        return industry;
    }

    public void setIndustry(String industry) {
        this.industry = industry;
        notifyPropertyChanged(BR.industry);
    }

    public PersonalHome(Context context, int uid) {
        super(context);
        this.uid = uid;
        super.getHandler().setTitle("个人主页");
        this.infoSelected = true;
        articles = new ListBindingAdapter<ArticleItem>(context, BR.article);
        load();
    }

    /**
     * 获取个人中心信息
     */
    public void load() {
        LoadingDialog.getInstance(getContext()).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
//                "zone":null,"industry":null,"introduction":nul
                LinkedTreeMap map = WebApi.getExpertDetail(getContext(), uid);
                if (map != null) {
                    setJob((String) map.get("job"));
                    setAttentioncoun(Utils.getIntValue(map.get("gzcou")));
                    setFanscoun(Utils.getIntValue(map.get("fscou")));
                    setRate(Utils.getDoubleValue(map.get("judgerank")));
                    setCompany((String) map.get("company"));
                    setAttentioned(Utils.getBooleanValue(map.get("gzflag")));
                    setJob((String) map.get("job"));
                    setUserName((String) map.get("nickname"));
                    setRealName((String) map.get("realname"));
                    setAvatar((String) map.get("avatar"));
                    setExpert(Utils.getBooleanValue(map.get("expertflag")));
                    setBackground((String) map.get("backgroundimg"));
                }
                map = WebApi.getExpertIntroduction(getContext(), uid);
                setZone((String) map.get("zone"));
                setIndustry((String) map.get("industry"));
                setIntroduction((String) map.get("introduction"));
                LoadingDialog.getInstance(getContext()).dismiss();
            }
        }).start();
    }

    /**
     * 加载文章列表
     */
    public void loadArticle() {
        final ListBindingAdapter adapter = getArticles();
        adapter.clear(true);
        LoadingDialog.getInstance(getContext()).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                adapter.setVariableId(BR.article);
                final Activity activity = (Activity) getContext();
                ArrayList arrayList = WebApi.getExpertArticleList(activity, getUid());
                try {
                    for (Object item : arrayList) {
                        LinkedTreeMap map = (LinkedTreeMap) item;
                        ArticleItem articleItem = new ArticleItem(getContext(), R.layout.i_article);
//                        CommonListItem articleItem = new CommonListItem(getContext(), R.layout.i_article, uid == User.getCurrentUser().getUid());
                        articleItem.setTitle((String) map.get("title"));
                        articleItem.setAvatar(getAvatar());
                        articleItem.setTag(getJob());
                        articleItem.setLikecoun(Utils.getIntValue(map.get("doclikecount")));
                        articleItem.setReplycoun(Utils.getIntValue(map.get("commentcount")));
                        articleItem.setAuthor(getUserName());
                        articleItem.setArticleId(Utils.getIntValue(map.get("articleid")));
                        articleItem.setUploadtime(Utils.getDateFromFormat(map.get("systime"), "yyyy/MM/dd HH:mm:ss"));
//                        articleItem.setType(CommonListItem.TYPE_ARTICLE);
//                        ArticleItem articleItem = new ArticleItem(activity, R.layout.i_article);
//                        articleItem.setAvatar((String) map.get("avatar"));
//                        articleItem.setTitle((String) map.get("title"));
//                        articleItem.setArticleId(Utils.getIntValue(map.get("articleid")));
//                        articleItem.setUploadtime(Utils.getDateFromFormat(map.get("systime"), "yyyy/MM/dd HH:mm:ss"));
//                        articleItem.setReplycoun(Utils.getIntValue(map.get("commentcount")));
//                        articleItem.setLikecoun(Utils.getIntValue(map.get("doclikecount")));
//                        ArrayList content = (ArrayList) map.get("content");
//
//                        articleItem.setDescription(content.size() > 0 ? (String) ((LinkedTreeMap) content.get(0)).get("content") : "");
//                        // 图片列表
//                        ArrayList imageList = (ArrayList) map.get("contentimg");
//                        ListBindingAdapter images = articleItem.getImageList();
//                        images.setVariableId(BR.src);
//
//                        for (Object image : imageList) {
//                            LinkedTreeMap treeMap = (LinkedTreeMap) image;
//                            ImageItem imageItem = new ImageItem(getContext());
//                            imageItem.setSrc((String) treeMap.get("content"));
//                            images.add(imageItem);
//                            if (images.getCount() > 5) {
//                                break;
//                            }
//                        }

                        adapter.add(articleItem);
                    }
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                notifyDataSetChanged(adapter);
                LoadingDialog.getInstance(getContext()).dismiss();
            }
        }).start();
    }

    /**
     * 点击向他咨询
     *
     * @param view
     */
    public void onClickAdvisory(View view) {
        if (expert) {
            LoadingDialog.getInstance(getContext()).show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (!WebApi.allowAction(getContext())) {
                        LoadingDialog.getInstance(getContext()).dismiss();
                        return;
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Context context = getContext();
                            AdviceInfo adviceInfo = new AdviceInfo(context, getRealName(), "专家", false);
                            adviceInfo.setExpertId(uid);
                            AdviceInfo.openAdvice(adviceInfo);
                        }
                    });
                    LoadingDialog.getInstance(getContext()).dismiss();
                }
            }).start();
        }
    }

    /**
     * 点击加关注
     *
     * @param view
     */
    public void onClickAttention(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!attentioned) {
                    if (WebApi.addAttention(getContext(), uid)) {
                        showToast("关注成功");
                        setAttentioned(true);
                    }
                } else {
                    if (WebApi.unAttention(getContext(), uid)) {
                        showToast("已取消关注");
                        setAttentioned(false);
                    }
                }
            }
        }).start();
    }

    public void onClickShare(View view) {
        ShareModel model = new ShareModel(getContext());
        model.setShareCallback(new ShareModel.Callback() {
            @Override
            public void onShareSuccess() {

            }

            @Override
            public void onShare(ShareItem shareItem) {
                ShareContent content = new ShareContent();
                content.mTargetUrl = WebApi.getShareUrl();
                content.mTitle = ShareModel.SHARE_TITLE;
                content.mText = ShareModel.SHARE_TEXT;
                content.mMedia = new UMImage(getContext(), ShareModel.SHARE_IMG);
                shareItem.setShareData(content);
            }
        });
        Activity activity = (Activity) getContext();
        PopupActivity.startActivity(activity, R.layout.v_dialog_share, BR.share, model);
    }

    public void onClickFans(View view) {
        MyFansModel model = new MyFansModel(getContext(), getUid());
        CommonFragment fragment = new CommonFragment();
        fragment.setDataModel(model);
        fragment.setVariableId(BR.fans);
        fragment.setLayoutId(R.layout.f_my_fans);
        HomeActivity.switchFragment((Activity) getContext(), fragment);
    }

    public void onClickFollow(View view) {
        MyFollowModel model = new MyFollowModel(getContext(), getUid());
        CommonFragment fragment = new CommonFragment();
        fragment.setDataModel(model);
        fragment.setVariableId(BR.follows);
        fragment.setLayoutId(R.layout.f_my_follow);
        HomeActivity.switchFragment((Activity) getContext(), fragment);
    }
}
